﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Google.GData.Calendar;

namespace Journal.Helpers
{
    /// <summary>
    /// Interaction logic for Event.xaml
    /// </summary>
    public partial class EventWindow : Window
    {

        private Domain.Event thisEvent;
        private bool canceled = true;
        private DateTime startTime;
        private DateTime endTime;
        enum WrongTime { Format = 0, Order = 1 }

        public EventWindow(string title, Domain.Event thisEvent, ItemCollection calendars)
        {
            this.Title = title;
            this.thisEvent = thisEvent.Clone() as Domain.Event;
            InitializeComponent();
            this.textBoxName.Focus();

            foreach (Domain.Calendar item in calendars)
            {
                this.comboBoxCalendar.Items.Add(item);
            }
            this.textBoxStartTime.Text = Utilities.ParseTimeElementForTextBox(this.thisEvent.StartTime.Hour)
                + ":" + Utilities.ParseTimeElementForTextBox(this.thisEvent.StartTime.Minute);
            this.textBoxEndTime.Text = Utilities.ParseTimeElementForTextBox(this.thisEvent.EndTime.Hour)
                + ":" + Utilities.ParseTimeElementForTextBox(this.thisEvent.EndTime.Minute);
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            this.DataContext = this.thisEvent;
        }

        public Domain.Event ShowAndRetrieveEvent()
        {
            this.ShowDialog();
            if (this.canceled) return null;
            else
            {
                this.thisEvent = this.DataContext as Domain.Event;

                // If 'All day' property is true, set precise times to StartTime and EndTime to 'thisEvent'
                if (!this.thisEvent.AllDay)
                {
                    this.thisEvent.StartTime = this.startTime;
                    this.thisEvent.EndTime = this.endTime;
                }

                return this.thisEvent;
            }
        }

        private void toggleAllDay()
        {
            this.textBoxStartTime.Visibility =
                this.checkBoxAllDay.IsChecked.Value ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
            this.textBoxEndTime.Visibility =
                this.checkBoxAllDay.IsChecked.Value ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
        }

        private void toggleIncorrectTimeLabel(bool show, WrongTime error)
        {
            if (show)
            {
                string msg = "";
                switch (error)
                {
                    case WrongTime.Format: msg = Utilities.GetLocalizedValue("Journal", "Locale", "EventWindow_TimeValidation_Format"); break;
                    case WrongTime.Order: msg = Utilities.GetLocalizedValue("Journal", "Locale", "EventWindow_TimeValidation_Order"); break;
                }
                this.labelError.Content = msg;
            }

            this.labelError.Visibility = show ? Visibility.Visible : Visibility.Collapsed;
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            this.startTime = Utilities.ValidateAndGetTime(this.thisEvent.StartTime, this.textBoxStartTime.Text);
            this.endTime = Utilities.ValidateAndGetTime(this.thisEvent.EndTime, this.textBoxEndTime.Text);

            if (this.startTime.Ticks == 0)
            {
                this.textBoxStartTime.Focus();
                toggleIncorrectTimeLabel(true, WrongTime.Format);
            }
            else if (this.endTime.Ticks == 0)
            {
                this.textBoxEndTime.Focus();
                toggleIncorrectTimeLabel(true, WrongTime.Format);
            }
            else if (this.endTime.CompareTo(this.startTime) < 0)
            {
                toggleIncorrectTimeLabel(true, WrongTime.Order);
            }
            else
            {
                this.canceled = false;
                this.Close();
            }
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void checkBoxAllDay_Checked(object sender, RoutedEventArgs e)
        {
            toggleAllDay();
        }

        private void checkBoxAllDay_Unchecked(object sender, RoutedEventArgs e)
        {
            toggleAllDay();
        }
    }
}
